<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 使用工厂函数
         * -创建饮水机对象，饮水机对象有热水和冷水方法，以及饮水机位置属性
         */
        function hotwater() {
            alert('热水')
        }
        function coldwater() {
            alert('冷水')
        }
        function waterDispenser(position) {
            var obj = new Object();
            obj.position = position;
            obj.hotwater = hotwater;
            obj.coldwater - coldwater;
            return obj;
        }
        var dispenser = waterDispenser('墙角');
        dispenser.hotwater();

        //  创建三个怪物对象，并且输出怪物的名字、性别、等级，怪物执行攻击方法，输出“水球术”
        // function skill01() {
        //     alert(this._skill01)
        // }
        function createMonster(name, sex, grade) {
            var obj = new Object();
            obj.name = name;
            obj.sex = sex;
            obj.grade = grade;
            obj.skill01 = function () {
                alert('水球术')
            }
            return obj;
        }

        var monster01 = createMonster('哭哭怪', '男', 'S级');
        var monster02 = createMonster('流泪怪', '女', 'A级');
        var monster03 = createMonster('嗷嗷怪', '男', 'D级');
        monster01.skill01();
        monster02.skill01();

        // 创建一个动漫对象，输出动漫的人物，爱好，性别，能力
        function cartoonCharacter(name, sex, hobby, skill01) {
            var obj = new Object();
            obj.name = name;
            obj.sex = sex;
            obj.hobby = hobby;
            obj.skill = skill01;
            return obj;
        }
        var character = cartoonCharacter('五条悟', '男', '爱我', function () {
            alert('领域展开')
        });
        console.log(character);
        character.skill();


        // 创建一个电脑对象，有颜色(color)、重量(weight)、品牌(brand)、型号(type)等属性，有看电影(watch)、听音乐(listen)、打游戏(play)和敲代码(coding)等方法。
        // 颜色、重量、品牌、型号是属性；看电影、听音乐、打游戏、敲代码是方法，在调用方法时分别打印 “我在看电影 / 听音乐 / 打游戏 / 敲代码”
        // function skill() {
        //     alert('我在' + this.watch);
        // }
        function MyComputer(color, weight, brand, type, watch, listen, play, coding) {
            this.color = color;
            this.weight = weight;
            this.brand = brand;
            this.type = type;
            this.watch = watch;
            this.listen = listen;
            this.play = play;
            this.coding = coding;
            // this.skill = skill;
        }
        MyComputer.prototype.skill01 = function () {
            alert('我在' + this.watch);
        }
        MyComputer.prototype.skill02 = function () {
            alert('我在' + this.listen);
        }
        MyComputer.prototype.skill01 = function () {
            alert('我在' + this.play);
        }
        MyComputer.prototype.skill01 = function () {
            alert('我在' + this.coding);
        }
        var property = new MyComputer('黑色', '0.5kg', '戴尔', '戴尔xps系列', '看电影', '听音乐', '打游戏', '敲代码');
        property.skill01();

        var data = [
            {
                StudentNumber: 2022003,
                Chinese: 86,
                Math: 64,
                English: 80,
                TotalScore: "",
                Comment: ""
            },

            {
                StudentNumber: 2022004,
                Chinese: 78,
                Math: 99,
                English: 91,
                TotalScore: "",
                Comment: ""
            },
            {
                StudentNumber: 2022005,
                Chinese: 107.5,
                Math: 97,
                English: 70,
                TotalScore: "",
                Comment: ""
            },
            {
                StudentNumber: 2022006,
                Chinese: 112,
                Math: 61,
                English: 92,
                TotalScore: "",
                Comment: ""
            },
            {
                StudentNumber: 2022007,
                Chinese: 101,
                Math: 79,
                English: 104,
                TotalScore: "",
                Comment: ""
            },
            {
                StudentNumber: 2022008,
                Chinese: 71,
                Math: 72,
                English: 105,
                TotalScore: "",
                Comment: ""
            },
            {
                StudentNumber: 2022009,
                Chinese: 56,
                Math: 68,
                English: 61,
                TotalScore: "",
                Comment: ""
            },
            {
                StudentNumber: 2022010,
                Chinese: 98,
                Math: 83,
                English: 77,
                TotalScore: "",
                Comment: ""
            }
        ];

        //一般流程
        // 以上是某班级一次考试的成绩表。请计算每个学生总成绩，并按总成绩排名。统计各单科成绩第一名，输出其成绩与学号。
        for (let stu of data) {
            stu.TotalScore = stu.Chinese + stu.Math + stu.English;
        }
        //总成绩第一名的学生学号
        data.sort(function (a, b) {
            return b.TotalScore - a.TotalScore;
        });
        console.log('总成绩第一名的学生的学号为:' + data[0].StudentNumber + ',总成绩第一名的学生的总成绩为:' + data[0].TotalScore);
        //语文单科成绩第一名的学生学号
        data.sort(function (a, b) {
            return b.Chinese - a.Chinese;
        });
        console.log('语文成绩第一名的学生的学号为:' + data[0].StudentNumber + ',语文成绩第一名的学生的语文成绩为:' + data[0].Chinese);
        // 并根据总成绩计算每个学生成绩备注保存在 Comment 中，60分以上为D，70分以上为C，80分以上为B，90分以上为A，60分以下为E
        for (let stu of data) {
            stu.TotalScore = stu.Chinese + stu.Math + stu.English;
            var stuscore = (stu.TotalScore / 3).toFixed(1);
            if (stuscore >= 90) {
                stu.Comment = 'A';
            } else if (stuscore >= 80) {
                stu.Comment = 'B';
            } else if (stuscore >= 70) {
                stu.Comment = 'C';
            } else if (stuscore >= 60) {
                stu.Comment = 'D';
            } else {
                stu.Comment = 'E';
            }
        }
        console.log(data);

        //构造函数
        function ScoreHandling(data) {
            this.data = data;
        }
        ScoreHandling.prototype.scoreSort = function (data, subject, subjectText) {
            data.sort(function (a, b) {
                return b[subject] - a[subject];
            })
            document.write(subjectText + '第一名是：' + data[0].StudentNumber + '；成绩值为：' + data[0][subject]);
            document.write('<br>');
        }
        ScoreHandling.prototype.outputScore = function () {
            let _data = this.data;
            for (let stu of data) {
                stu.TotalScore = stu.Chinese + stu.Math + stu.English;
                var stuscore = (stu.TotalScore / 3).toFixed(1);
                if (stuscore >= 90) {
                    stu.Comment = 'A';
                } else if (stuscore >= 80) {
                    stu.Comment = 'B';
                } else if (stuscore >= 70) {
                    stu.Comment = 'C';
                } else if (stuscore >= 60) {
                    stu.Comment = 'D';
                } else {
                    stu.Comment = 'E';
                }
            }
        }
        console.log(_data);
        this.scoreSort(_data, 'TotalScore', '总成绩');
        this.scoreSort(_data, 'Chinese', '语文');
        this.scoreSort(_data, 'Math', '数学');
        this.scoreSort(_data, 'English', '英语');

    </script>
</body>

</html>